Population

Population#

In this Notebook, we will be looking at the population and it’s distribution in Freiberg. The Data is from the 2011 Census.

cfg = ConfigObj(r"..\notebook.ini")
csv10m = Path((Path(".").parent), "csvs", "Freiberg, Sachsen-10m.csv")
df = pd.read_csv(csv10m)
df.columns = ["id", "lon", "lat", "pop"]
df = df.set_index("id")
df
lon lat pop
id
0 13.327293 50.875905 0.80
1 13.327435 50.875905 0.80
2 13.327577 50.875905 0.80
3 13.327719 50.875905 0.80
4 13.327862 50.875905 0.80
... ... ... ...
107095 13.330049 50.944991 0.03
107096 13.330191 50.944991 0.03
107097 13.330333 50.944991 0.03
107098 13.330475 50.944991 0.03
107099 13.330617 50.944991 0.03

107100 rows × 3 columns

fig, ax = plt.subplots(figsize=(16, 12))
ax = df.plot.hexbin(x="lon",
                    y="lat",
                    C="pop",
                    reduce_C_function=np.sum,
                    gridsize=50,
                    ax=ax)
../_images/ef8c73d9c06d5ce9cb0d3063fcd00db291ade27574bd78dc88457ff055f5cae8.png
tags = {"type": "boundary"}
place = "Freiberg, Sachsen"
# gdf = ox.geometries_from_place(, tags)
# gdf.plot()
bbox = ox.geocoder.geocode_to_gdf(place, buffer_dist=200)
geom = bbox.geometry.loc[0]

p = gpd.GeoSeries(geom).geometry.boundary
ax = p.plot(color=None, edgecolor='k', linewidth=2, ax=ax)
figure = ax.get_figure()
figure
../_images/554121ff2fa091bae8b270d14cd1683b57f7e6511c45eed4f28194a7828b788b.png
<Figure size 640x480 with 0 Axes>
def heat_map(df):
    # lat/long/weight

    try:
        data = df[["lat", "lon", "pop"]]
    except KeyError:
        data = df[["x", "y", "einwohner"]]

    # print(df["2"].max())

    m = folium.Map([50.9, 13.37], zoom_start=12)

    HeatMap(data, min_opacity=0.4,
            gradient={.4: 'blue', .8: 'lime',
                      0.9: 'yellow', 0.95: 'orange', 1: 'red'}
            ).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
    folium.LayerControl().add_to(m)

    # m.save('{0}.html'.format('Freiberg-census-10m'))
    return m
heat_map(df)
Make this Notebook Trusted to load map: File -> Trust Notebook
cfg = ConfigObj("notebook.ini")
scenarios = cfr.get_scenarios(cfg)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Small")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/4295b21072073721365e0ad79a914328bb308c6ee03e3e0f2e5925e632e7a104.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Small")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/4295b21072073721365e0ad79a914328bb308c6ee03e3e0f2e5925e632e7a104.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Medium")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/d4f8b11eb041797dede8710fa97358da278ea116386a77128ad613b6fed3f478.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Medium-restricted")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/2fafbe2d9b84dc7a34e0f5e5d4eb08ca17da9b02d24c737ef5380032b8ccab04.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Large")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/343e2695f1c635a8f88ec65606e0b49704719f53c7d2d02e592e223e84784a5e.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)
G = oh.get_place(placename=place, simplify=True,
                 clean_periphery=True,
                 file_identifier="", retain_all=True)
G_nx = r4r.load_pop_scenario(cfg, G, place, "Optimal")
to_remove = []
for node in G_nx.nodes():
    try:
        G_nx.nodes[node]["pop"]
    except KeyError:
        to_remove.append(node)

G.remove_nodes_from(to_remove)
x = ox.plot_graph(G,
                  figsize=(16, 12),
                  node_color=ox.plot.get_node_colors_by_attr(G, "pop"),
                  node_size=5,
                  edge_linewidth=0.1)
x
../_images/1d2f3912035a6ac549c4cd3c9cbc899614b13d0e7239d6d21d7fcbe1979eb076.png
(<Figure size 1600x1200 with 1 Axes>, <Axes: >)